<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use DB;

class searchController extends Controller
{
    //搜索商品页面
    public function index(Request $request)
    {
        // 保存搜索的条件
        $where = [];

        //商品遍历
        $goods = DB::table('goods')
            ->join('goodscate', 'goodscate.id', '=', 'goods.cid')
            ->join('goodsmain', 'goodsmain.gid', '=', 'goods.id')
            ->select('goods.name','goods.status','goodscate.status','goodsmain.hwprice','goodsmain.price','goodsmain.status','goodsmain.pic0')
            ->where('goods.status', '1')
            ->where('goodscate.status', '1')
            ->where('goodsmain.status', '1');
       
        if($request->has('name')){
            $goods->where('goods.name','like',"%{$request->input('name')}%");
            $where['name'] = $request->input('name');
        }

        $gcdatas = DB::table('goodscate')->where('status', '1')->get();
        $goodsId = DB::table('goods')->where('status', '1')->get();
        
        $goods = $goods->paginate(10);

        return view('home.search',['goods' => $goods,'gcdatas' => $gcdatas,'goodsId' => $goodsId,'where'=>$where]);
    }
}
